#!/bin/bash
# ======================================================================
#
#   Copyright (C) 2006 and onward Robert Muth <robert at muth dot org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of June 1991.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program in form of the file COPYING;
#   if not, write to the
#
#   Free Software Foundation, Inc. <http://www.fsf.org>
#   59 Temple Place, Suite 330,
#   Boston, MA 02111-1307  USA
#
# ======================================================================

# This is a simple tool for the internationlization of pyTrommler
# To use it you will need the gnu gettext package.
# To install it on an ubuntu systems, say: sudo apt-get install gettext

# In order to create a new translation, e.g. spanish, do
# * create new .po file for your locale by running
#   './internationalization_tool.sh new_language es'
# * edit the file printed by the previous command
# * update the locale files by running
#   './internationalization_tool.sh install_all_translations'
# * test the new translations by running
#   './internationalization_tool.sh test es'


set -o nounset
set -o errexit

LOCALEDIR=locale

XGETTEXT=xgettext
MSGMERGE=msgmerge
MSGFMT=msgfmt

DOMAIN=pytrommler

POTFILES=${LOCALEDIR}/POTFILES
POT=${LOCALEDIR}/${DOMAIN}.pot

StripExtension ()
{
     echo ${1%.*}
}

ExtractLanguage ()
{
    base=$(basename $1)
    echo $(StripExtension $base)
}

UpdateTranslation ()
{
    ${MSGMERGE} --quiet --update --no-wrap $1 ${POT}
}

InstallTranslation ()
{
    ${MSGFMT} --output=${1/.po/.mo} $1
}

CreateNewTranslation ()
{
    path=${LOCALEDIR}/$1/LC_MESSAGES
    filename=$path/${DOMAIN}.po
    if [ -e $filename ] ; then
	echo "ERROR:language $1 already exists at $filename"
	exit
    fi

    mkdir -p $path
    cp ${POT} $filename
    echo
    echo "A new po file named  $filename has been created."
    echo
    echo "Please edit with your translations using a utf-8 editor."
    echo
}

UpdatePot()
{
    ${XGETTEXT}\
        --language=Python\
        --default-domain=${DOMAIN}\
        --directory=.\
        --sort-by-file\
        --files-from=${POTFILES}\
        --keyword=_\
        --output=${POT}
}


MODE=${1:-none}
shift


if [ $MODE = 'update_pot' ] ; then
    UpdatePot
    for po in ${LOCALEDIR}/*/LC_MESSAGES/${DOMAIN}.po ; do
	echo "Updating $po"
	UpdateTranslation $po
    done
    exit 0
fi


if [ $MODE = 'new_language' ] ; then
    lang=$1
    UpdatePot
    CreateNewTranslation $lang
    exit 0
fi


if [ $MODE = 'install_all_translations' ] ; then
    for po in ${LOCALEDIR}/*/LC_MESSAGES/${DOMAIN}.po ; do
	echo "Installing $po"
	InstallTranslation $po
    done
    exit 0
fi


if [ $MODE = 'test' ] ; then
    lang=$1
    export LANG=$1 ; ./pytrommler.py
    exit 0
fi


echo "unknown mode $MODE"
exit -1
